import java.util.Scanner;

/*
 * Task 3. Anagrams
 */

/**
 * The program should receive input of two strings and give the output to screen
 * telling whether these words are anagrams
 * 
 * @version 0.2 24 Jan 2013
 * @author Tymur Lysak
 */
public class Anagram {
	private String firstPhrase;
	private String secondPhrase;

	public static void main(String[] args) {
		Anagram anagramFinder = new Anagram();
		anagramFinder.setFirstPhrase(getPhraseFromPromt("Please enter the first phrase"));
		anagramFinder.setSecondPhrase(getPhraseFromPromt("Please enter the second phrase"));
		boolean result = anagramFinder.areAnagrams();
		anagramFinder.printResultToPromt(result);
	}

	private void printResultToPromt(boolean result) {
		if (result) {
			System.out.println("the phrases are anagrams");
		} else {
			System.out.println("the phrases are not anagrams");
		}
	}

	public static String getPhraseFromPromt(String messageToUser) {
		System.out.println(messageToUser);
		return new Scanner(System.in).nextLine();
	}

	public boolean areAnagrams() {

		if (stringSumHashCode(firstPhrase) == stringSumHashCode(secondPhrase)) {
			return true;
		} else
			return false;
	}

	private int stringSumHashCode(String string) {
		string = normalizeString(string);
		String[] procStringArray = string.split("");
		int stringSumHashCode = 0;
		for (int i = 0; i <= procStringArray.length - 1; i++) {
			stringSumHashCode += procStringArray[i].hashCode();
		}
		return stringSumHashCode;
	}
	
	private String normalizeString(String string) {
		string = string.replace(" ", "");
		string = string.toLowerCase();
		return string;
	}
	public void setFirstPhrase(String firstPhrase) {
		this.firstPhrase = firstPhrase;
	}

	public void setSecondPhrase(String secondPhrase) {
		this.secondPhrase = secondPhrase;
	}

}
